package info.webest.reminder.contacts;

import info.webest.bitmap.wbBitmap;
import info.webest.reminder.R;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ContanctAdapter extends ArrayAdapter<Contact> {

	private Activity activity;
	private List<Contact> items;
	private int row;
	private Contact contact;

	public ContanctAdapter(Activity act, int row, List<Contact> items) {
		super(act, row, items);

		this.activity = act;
		this.row = row;
		this.items = items;

		ContactPhoto.createDefaultBitmap(activity);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View view = convertView;
		ViewHolder holder;
		if (view == null) {
			LayoutInflater inflater = (LayoutInflater) activity
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(row, null);

			holder = new ViewHolder();
			view.setTag(holder);

		} else {
			holder = (ViewHolder) view.getTag();
		}

		if ((items == null) || ((position + 1) > items.size()))
			return view;

		contact = items.get(position);

		// holder.contactId = contact.getContactId();
		holder.tvName = (TextView) view.findViewById(R.id.tvTitle);
		holder.tvAge = (TextView) view.findViewById(R.id.tvAge);
		holder.tvEventDate = (TextView) view.findViewById(R.id.tvEventDate);
		// holder.tvEventType = (TextView)
		// view.findViewById(R.id.tvEventType);
		holder.ivPhoto = (ImageView) view.findViewById(R.id.ivPhoto);

		// Photo
		Bitmap bmp = ContactPhoto.getContactBitmap(activity,
				contact.getContactId(), contact.getName());

		if (bmp == null) {
			// holder.ivPhoto.setBackgroundColor(Color.GRAY);
			bmp = ContactPhoto.getDefaultBitmap();
		}
		bmp = wbBitmap.getRoundedCornerBitmap(bmp, Color.GRAY, 10, 4, activity);

		holder.ivPhoto.setImageBitmap(bmp);

		if (holder.tvName != null && null != contact.getName()
				&& contact.getName().trim().length() > 0) {
			holder.tvName.setText(Html.fromHtml(contact.getName()));
		}

		if (holder.tvEventType != null) {
			holder.tvEventType.setText("" + contact.getEventType());
		}

		if (holder.tvEventDate != null && null != contact.getEventDate()) {

			if (contact.getDateWithYear()) {
				holder.tvEventDate.setText(String.valueOf(contact
						.getEventFormattedDate("d MMMM yyyy")));
			} else {
				holder.tvEventDate.setText(String.valueOf(contact
						.getEventFormattedDate("d MMMM")));
			}
		}

		if (contact.getAge() == null) {
			holder.tvAge.setVisibility(View.GONE);
		} else {
			// (holder.tvAge != null && null != contact.getAge()
			// && contact.getAge().trim().length() > 0) {

			// int ageLength = contact.getAge().trim().length();
			// if (ageLength == 1){

			// }

			//if (!contact.getDateWithYear()) {
			//	holder.tvAge.setVisibility(View.GONE);
			//} else {
				holder.tvAge.setVisibility(View.VISIBLE);
				holder.tvAge.setText(Html.fromHtml(contact.getAge()));

				switch (contact.getEventType()) {
				case 1:
					holder.tvAge.setBackgroundResource(R.drawable.counter_blue);
					break;
				case 3:
					holder.tvAge
							.setBackgroundResource(R.drawable.counter_orange);
					break;
				}
			//}
		}

		// COLORED UPCOMING EVENTS

		int interval = Integer.valueOf(contact.getSortIndex())
				- ContactsReader.getTodaySortIndex();
		if (interval == 0) {

			holder.tvAge.setBackgroundResource(R.drawable.counter_red);
			/*
			 * switch (Options.Theme) { case THEME_BLACK:
			 * view.setBackgroundColor(getResources().getColor(
			 * R.color.black_today)); break;
			 * 
			 * case THEME_LIGHT:
			 * view.setBackgroundColor(getResources().getColor(
			 * R.color.light_today)); break; }
			 */

		} else if ((interval > 0) && (interval < 7)) {

			/*
			 * switch (Options.Theme) { case THEME_BLACK:
			 * view.setBackgroundColor(getResources().getColor(
			 * R.color.black_soon)); break;
			 * 
			 * case THEME_LIGHT:
			 * view.setBackgroundColor(getResources().getColor(
			 * R.color.light_soon)); break;
			 */
		}

		else {

			/*
			 * switch (Options.Theme) { case THEME_BLACK:
			 * view.setBackgroundColor(getResources().getColor(
			 * R.color.black_row)); break;
			 * 
			 * case THEME_LIGHT:
			 * view.setBackgroundColor(getResources().getColor(
			 * R.color.light_row)); break; }
			 */
			// view.setBackgroundColor(Color.TRANSPARENT);
		}

		return view;
	}

	@Override
	public int getCount() {
		return this.items.size();
	}

	public class ViewHolder {
		// private int contactId;
		public TextView tvName, tvPhoneNo, tvEventType, tvEventDate, tvAge;
		public ImageView ivPhoto;
	}
}